package org.daochong.web;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.daochong.lang.BeanUtil;
import org.daochong.lang.LocaleContext;

public class WebContext {

	public static String SKIN_ID = "skin";
	public static String LOCALE_ID = "locale";
	public static String MESSAGE_ID = "message";
	public static String RESOURCES_ID = "resources";

	private static ThreadLocal<WebContextBean> context = new ThreadLocal<WebContextBean>();

	private static ServletContext servletContext;

	public static ServletContext getServletContext() {
		return servletContext;
	}

	public static String getBasePath() {
		return servletContext.getRealPath("/");
	}

	public static String getRealPath(String path) {
		return servletContext.getRealPath(path);
	}

	public static String getContextPath() {
		return servletContext.getContextPath();
	}

	public static Object getAttribute(String id) {
		WebContextBean bean = context.get();
		if (bean != null) {
			return bean.map.get(id);
		}
		return null;
	}

	public static void setObject(String id, Object obj) {
		WebContextBean bean = context.get();
		if (bean != null) {
			bean.map.put(id, obj);
		}
	}

	@SuppressWarnings("unchecked")
	public static <T> T getAttribute(String id, Class<T> clazz) {
		WebContextBean bean = context.get();
		if (bean != null) {
			Object o = bean.map.get(id);
			if (o != null) {
				if (BeanUtil.isMatch(o, clazz)) {
					return (T) o;
				}
			}
		}
		return null;
	}

	public static String getHost() {
		HttpServletRequest request = getRequest();
		if (request != null) {
			return WebUtils.getHost(request);
		}
		return null;
	}

	public static HttpServletRequest getRequest() {
		WebContextBean bean = context.get();
		if (bean != null) {
			return bean.getRequest();
		}
		return null;
	}

	public static HttpServletResponse getResponse() {
		WebContextBean bean = context.get();
		if (bean != null) {
			return bean.getResponse();
		}
		return null;
	}

	public static String getSkin() {
		WebContextBean bean = context.get();
		if (bean != null) {
			return bean.getSkin();
		}
		return null;
	}

	public static Locale getLocal() {
		return LocaleContext.getLocale();
	}

	protected static void setContext(HttpServletRequest request, HttpServletResponse response, String skin) {
		WebContextBean bean = new WebContextBean();
		bean.setRequest(request);
		bean.setResponse(response);
		bean.setSkin(skin);
		context.set(bean);
	}

	protected static void setServletContext(ServletContext context) {
		servletContext = context;
	}
}

class WebContextBean {
	private HttpServletRequest request;
	private HttpServletResponse response;
	private String skin;
	protected Map<String, Object> map = new HashMap<String, Object>();

	public String getSkin() {
		return skin;
	}

	public void setSkin(String skin) {
		this.skin = skin;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

}
